package com.yigang.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yigang.entity.ProductImg;
import com.yigang.service.ProductImgService;
import com.yigang.utils.FileUploadUtils;
import com.yigang.utils.ResultUtil;
/**
 * @author 陈佳祥
 *商品图片管理  管理
 */
@Controller
@RequestMapping("/productImg")
public class ProductImgController {

	@Autowired
	private ProductImgService productImgService;
	//妞ょ敻娼伴柌宥呯暰閸氾拷
	@RequestMapping("productImgList")
	public String productImgList() {
		return "jsp/productImg/productImgList";
	}
	@RequestMapping("addProductImg")
	public String addProductImg() {
		return "jsp/productImg/addProductImg";
	}
	
	@RequestMapping("editProductImg/{productimg_id}")
	public String editProductImg(@PathVariable("productimg_id") int productimg_id, HttpSession session) {
		ProductImg productImg = productImgService.selectProductImgById(productimg_id);
	
		session.setAttribute("productImg", productImg);
		return "jsp/productImg/editProductImg";
	}
	
	//婢х偛鍨归弻銉︽暭閺傝纭�
	@RequestMapping("insertProductImg")
	@ResponseBody
	public ResultUtil insertProductImg(ProductImg record) {
		productImgService.insertSelective(record);
		return ResultUtil.ok();

	}

	@RequestMapping("deleteProductImgById")
	@ResponseBody
	public ResultUtil deleteProductImgById(int productimg_id) {
		
		return productImgService.deleteByPrimaryKey(productimg_id);
	}

	@RequestMapping("getProductImgList")
	@ResponseBody
	public ResultUtil getProductImgList(Integer page, Integer limit, ProductImg record) {

		return productImgService.selectProductImgList(page, limit, record);
	}

	@RequestMapping("updateProductImg")
	@ResponseBody
	public ResultUtil updateProductImg(ProductImg record) throws ParseException {
		
		return productImgService.updateByPrimaryKeySelective(record);
	}
	
	@PostMapping("/avatar")
	@ResponseBody
	public Map<String, String> avatar(MultipartFile file) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("avatar", "0");
		if (!file.isEmpty()) {

			String avatar = FileUploadUtils.upload("/proj_imgs/te", file);
			map.put("avatar", avatar);
			return map;
		}
		return map;
	}
	


}
